package com.jacen.etl.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jacen.etl.entity.EtlEntity;
import com.jacen.etl.entity.EtlScriptEntity;
import com.jacen.etl.services.EtlScriptService;
import com.jacen.etl.services.EtlService;
import com.jacen.etl.services.ServiceProxyFactory;
import com.jacen.etl.util.HttpParamCaster;
import com.jacen.etl.util.ServletUtils;
import com.jacen.etl.util.Utils;

public class EtlScriptManagerAction {

	private EtlScriptService service = ServiceProxyFactory.createProxy(EtlScriptService.class);
	private EtlService etlService = ServiceProxyFactory.createProxy(EtlService.class);
	private static final Logger logger = LoggerFactory.getLogger(EtlScriptManagerAction.class);
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @throws ServletException IOException
	 */
	public void getPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		int entityId = HttpParamCaster.getIntParameter(request, "entityId",0);
		int page = HttpParamCaster.getIntParameter(request, "page", 1);
		int size = HttpParamCaster.getIntParameter(request, "size",Utils.PAGE_SIZE);
		int entitySize = HttpParamCaster.getIntParameter(request, "entitySize",Utils.PAGE_SIZE);
		Map<String, Object> mapParams = new HashMap<String, Object>();
		mapParams.put("entityId", entityId);
		
		try {
			List<EtlScriptEntity> list = service.getPage(mapParams, page, size);
			long count = service.getCount(mapParams);
			long totalpage = (count/size) + ((count%size) == 0 ? 0 : 1);
			
			request.setAttribute("entityId", entityId);
			request.setAttribute("list", list);
			request.setAttribute("count", count);
			request.setAttribute("page", page);
			request.setAttribute("size", size);
			request.setAttribute("entitySize", entitySize);
			request.setAttribute("totalpage", totalpage);
		} catch (Exception e) {
			logger.error("获取EntityScript列表失败", e);
			request.setAttribute("error", e.getMessage());
			request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
			return;
		}
		request.getRequestDispatcher("/jsp/etlscript/list.jsp").forward(request, response);
	}
	
		/**
		 * 新增
		 * @param request
		 * @param response
		 * @return
		 * @throws IOException 
		 * @throws ServletException 
		 */
		public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			int entityId = 0;
			try {
				if (request.getMethod().equalsIgnoreCase("get")) {
					entityId = HttpParamCaster.getIntParameter(request, "entityId",0);
					request.setAttribute("entityId", entityId);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("entityId", entityId);
					long seqNo = service.getCount(map);
					request.setAttribute("scriptSeq", seqNo+1);
					request.getRequestDispatcher("/jsp/etlscript/insert.jsp").forward(request, response);
					return;
				}
				entityId = HttpParamCaster.getIntParameter(request, "entityId",0);
				EtlEntity etlEntity = etlService.getById(entityId);
				EtlScriptEntity scriptEntity = ServletUtils.getEntity(request, EtlScriptEntity.class);
				scriptEntity.setEntityId(etlEntity.getEntityId());
				scriptEntity.setCreateDate(new Timestamp(System.currentTimeMillis()));
				scriptEntity.setEntityName(etlEntity.getEntityName());
				scriptEntity.setEntitySchema(etlEntity.getEntitySchema());
				service.insert(scriptEntity);
			} catch (Exception e) {
				logger.error("新增EntityScript脚本失败", e);
				request.setAttribute("error", e.getMessage());
				request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
				return;
			}
			response.sendRedirect(request.getContextPath()+"/manage/etlScript?act=getPage&entityId="+entityId);
		}
		
		/**
		 * 修改
		 * @param request
		 * @param response
		 * @return
		 * @throws IOException 
		 * @throws ServletException 
		 */
		public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			String entityId  = "";
			try {
				String id = request.getParameter("scriptId");
				if(StringUtil.isEmpty(id)) throw new Exception("参数为空");
				EtlScriptEntity entity = service.getById(Integer.valueOf(id));

				if (request.getMethod().equalsIgnoreCase("get")) {
					request.setAttribute("script", entity);
					request.getRequestDispatcher("/jsp/etlscript/update.jsp").forward(request, response);
					return;
				}
				entityId = request.getParameter("entityId");
				EtlEntity etlEntity = etlService.getById(Integer.valueOf(entityId));
				ServletUtils.updateEntity(request, entity);
				entity.setUpdateDate(new Timestamp(System.currentTimeMillis()));
				entity.setEntityName(etlEntity.getEntityName());
				entity.setEntitySchema(etlEntity.getEntitySchema());
				service.update(entity);
			} catch (Exception e) {
				logger.error("更新操作失败", e);
				request.setAttribute("error", e.getMessage());
				request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
				return;
			}	
			response.sendRedirect(request.getContextPath()+"/manage/etlScript?act=getPage&entityId="+Integer.valueOf(entityId));
		}
		
		/**
		 * 删除
		 * @param request
		 * @param response
		 * @return
		 * @throws IOException 
		 * @throws ServletException 
		 */
		public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			String entityId  = "";
			try {
				entityId = HttpParamCaster.getUTF8Parameter(request, "entityId");
				String id = request.getParameter("id");
				EtlScriptEntity entity = service.getEntityByKey("script_id", id);
				service.delete(entity);
			} catch (Exception e) {
				logger.error("删除操作失败", e);
				request.setAttribute("error", e.getMessage());
				request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
				return;
			}	
			response.sendRedirect(request.getContextPath()+"/manage/etlScript?act=getPage&entityId="+Integer.valueOf(entityId));
		}
}
